package com.liyunc.toolset;

/**
 * Copyright (c) 2018-2023 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

/**
 * @author liyuncong
 * @version 1.0
 * @file Parser
 * @brief Parser
 * @details Parser
 * @date 2023-02-16
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2023-02-16                  liyuncong          Created
 */
public class Parser {

    public static final int defaultExitCodePatternFound = 0;
    public static final int defaultExitCodePatternNotFound = 1;
    public static final int defaultKillSignal = 9;

    public static CmdArgs parseCmdArgs(String args) {
        final String[] argsArray = args.split(" \t");
        return parseCmdArgs(argsArray);
    }

    public static CmdArgs parseCmdArgs(String[] args) {
        WorkMode workMode = null;
        SearchMode searchMode = null;
        String pattern = null;
        for (String arg : args) {
            if (arg.toLowerCase().contains(
                WorkMode.class.getSimpleName().toLowerCase())) {
                workMode = parseWorkMode(arg);
            }
            if (arg.toLowerCase().contains(
                SearchMode.class.getSimpleName().toLowerCase())) {
                searchMode = parseSearchMode(arg);
            }
            if (arg.toLowerCase().contains("pattern")) {
                pattern = parsePattern(arg);
            }
        }
        if (workMode == null) {
            throw new IllegalArgumentException("WorkMode must be not null");
        }
        if (searchMode == null) {
            throw new IllegalArgumentException("SearchMode must be not null");
        }
        if (pattern == null || pattern.length() == 0) {
            throw new IllegalArgumentException("Pattern must be not null");
        }
        return new CmdArgs(
            workMode,
            searchMode,
            pattern,
            defaultExitCodePatternFound,
            defaultExitCodePatternNotFound,
            defaultKillSignal
        );
    }

    public static WorkMode parseWorkMode(String arg) {
        if (arg.toLowerCase().contains(
            WorkMode.class.getSimpleName().toLowerCase())) {
            final String workModeName = arg.split("=")[1];
            return WorkMode.forName(trim(workModeName));
        }
        return null;
    }

    public static SearchMode parseSearchMode(String arg) {
        if (arg.toLowerCase().contains(
            SearchMode.class.getSimpleName().toLowerCase())) {
            final String searchModeName = arg.split("=")[1];
            return SearchMode.forName(trim(searchModeName));
        }
        return null;
    }

    public static String parsePattern(String arg) {
        if (arg.toLowerCase().contains("pattern")) {
            final String pattern = arg.split("=")[1];
            return trim(pattern);
        }
        return null;
    }

    public static String trim(String arg) {
        return arg.trim();
    }
}
